import {
    GETDATA,
    B
} from '../utils/R'
import {
    sumBy,
    ceil
} from 'lodash'

export default {
    namespace: 'leftBottomPage',
    state: {},
    subscriptions: { //监听   异步数据加载初始化
        setup({
            dispatch, //同步触发reducer
            history //路由参数
        }) {
            history.listen(({
                pathname
            }) => {
                //如果在根目录的时候 就直接执行一下异步方法获取数据
                if (pathname === '/one') {
                    dispatch({
                        type: 'GetDatas',
                    });
                }
            })
        }
    },
    effects: {
        * GetDatas({
            payload
        }, {
            call, //用于调用异步逻辑，支持 promise  
            put //异步触发reducer的action  修改state   使用是需要 yield put({})
        }) {
            const {
                data
            } = yield call(GETDATA, B)
            // 处理数据
            if (data) {
                // 计算总人数
                let Sums = sumBy(data, 'sums')
                let processData = data.map(v => {
                    return {
                        name: v.agevalue + ceil(v.sums / Sums * 100) + '%',
                        value: v.sums
                    }
                })
                yield put({
                    type: 'setDatas',
                    payload: processData,
                });
            }
        }
    }, //请求
    reducers: {
        setDatas(state, action) {
            return action.payload
        },
    }
}